<?php
/**
 * Create By
 * Author: jarming
 * Date: 2021/4/19
 * Time: 下午4:54
 */

class AES
{

    /**
     * AES/CBC/PKCS5Padding Encrypter
     *
     * @param $str
     * @param $key
     * @return string
     */
    static function encryptNew($str, $key)
    {
//        $zeroPack = pack('i*', 0);
//        $iv = str_repeat($zeroPack, 4);
        $ivLength = openssl_cipher_iv_length('AES-256-CBC');
        $iv = openssl_random_pseudo_bytes($ivLength, $isStrong);
        return base64_encode(openssl_encrypt($str, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv));
    }

    /**
     * AES/CBC/PKCS5Padding Decrypter
     *
     * @param $encryptedStr
     * @param $key
     * @return string
     */
    static function decryptNew($encryptedStr, $key)
    {
        $zeroPack = pack('i*', 0);
        $iv = str_repeat($zeroPack, 4);
        return openssl_decrypt(hex2bin($encryptedStr), 'AES-256-CBC', hex2bin(md5($key)), OPENSSL_RAW_DATA, $iv);
    }
}
var_dump(AES::encryptNew("abc",base64_decode("coDdNwo0sAkRWwwwYdcyRQ==")));